package com.anbinh.doctor.utils;

import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.PatientActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.DistrictAdapter;
import com.anbinh.doctor.adapter.SpinAdapter;
import com.anbinh.doctor.model.Address;
import com.anbinh.doctor.model.CityResponse;
import com.anbinh.doctor.model.CityResponse.Data.Item;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.DoctorInfo;
import com.anbinh.doctor.model.ItemMeu;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class Utils {
	public final static int LICH_KHAM = 0;
	public final static int SEND_MESSAGES = 1;
	public final static int CREATE_LH = 2;
	public final static int PATIENT = 3;
	public final static int ACCOUNT = 4;
	public final static int LICHHEN_SELECT_MONTH = 5;
	public final static int LICKKHAM_SELECT_MONTH = 6;
	public final static int UPDATE_LOCATION = 7;
	public final static int CREATE_ACC = 8;
	public final static int ACTIVE_ACC = 9;
	public final static int ADD_BANG_CAP = 10;
	public static final int EDIT_CHUCVU = 11;
	public static final int EDIT_BANGCAP = 12;
	public static final int EDIT_CHUYENKHOA = 13;
	public static final int EDIT_HIEPHOI = 14;
	public static final int VIEW_IMG = 15;
	public static final int SERVICE_CHILD = 16;
	public static final int APPOINTMENT_DETAILT = 17;
	public static final int CREATE_APPOINTMENT = 18;
	public static final int COMMENT_DETAILT = 19;
	public static final int ONRESUME_LICHHEN = 20;
	public static final int EDIT_BANGGIA = 21;
	public static final int TAM_HOAN = 22;
	public static final int RESUME_LOCATION = 23;
	public static final int RESUME_EXTRA_INFO = 24;
	public static final int RESUME_PRICE = 25;
	public static final int ADD_LOCATION = 26;
	public static final int RELOAD_ASSISTIANT = 27;
	public static final int SET_PERMISSION = 28;
	public static final int ASSISTANT_DETAILT = 29;
	public static final int RESUME_LOAD_LICH_KHAM = 30;
	public static final int SEARCH_LICH_HEN = 30;
	public static final int EDIT_LICH_KHAM = 31;
	public static final int SEARCH_LICHHEN = 32;
	public static final int SELECT_PHONE_CONTACT = 32;
	public static final int SHOW_PROCESS = 33;
	public static final int GONE_PROCESS = 34;
	public static final int SUSSCESS = 35;
	public static final int SEARCH_PATIENTS = 36;
	public static final int SEARCH_BENH_NHAN = 37;
	public static final int PATIENT_DETAILT = 38;
	public static final int ONRESUME_BIEU_HIEN = 39;
	public static final int ATTACH_FILE_REPLY = 40;
	public final static int REQ_EDIT_AVATAR = 41;
	public final static int REQ_ATTACH_FILE = 42;
	public final static int REPLY_PATIENT = 43;
	public static final int ATTACH_FILE_SEND_MESSAFE = 44;
	public static final int SEARCH_MESSAGE = 45;
	public static final int ADD_IMAGES_PATIENTS = 46;
	public static final int REQ_UPLOADIMG = 47;
	public static final int VIEW_IMG_PATIENT = 48;
	public static final int ADD_FAMILY_MEDICAL_HIS = 49;
	public static final int THEM_LICH_SU_KHAM = 50;
	public static final int ADD_NOTE = 51;
	public static final int DOCTOR_DETAILT = 52;
	public static final int UP_IMAGE_DOCTOR = 53;
	public static final int ADD_DOCTOR = 54;
	public static final int RELOAD_DOCTOR = 55;
	public static final int EXIST_MONTH = 56;
	public static final int SELECT_MONTH_LICH_HEN = 57;
	public static final int SELECT_MONTH_LICH_KHAM = 58;
	public static final int EDIT_LICH_KHAM_DOCTOR = 59;
	public static final int APPOINTMENT_DETAILT_DOCTOR = 60;
	public static final int CREATE_APPOINTMENT_DOCTOR = 61;
	public static final int SEARCH_LICH_HEN_DOCTOR = 62;
	public static final int SEARCH_BENH_NHAN_DOCTOR = 63;
	public static final int SEARCH_MESSAGE_DOCTOR = 64;
	public final static int SEND_MESSAGES_DOCTOR = 65;
	public final static int REPLY_PATIENT_DOCTOR = 66;
	public static final int ATTACH_FILE_REPLY_DOCTOR = 67;
	public static final int ATTACH_FILE_SEND_MESSAFE_DOCTOR = 68;
	public static final int SELECT_PHONE_CONTACT_ADD_ASSISTANT = 69;
	public static final int EDIT_BAIVIET = 70;
	public static final int MAIN = 71;
	public final static int LICH_KHAM_CLINIC = 72;
	public static final int SELECT_PHONE_CONTACT_ADDDOCTOR = 73;
	public static final int DELETE_MESSAGE = 74;
	public static final int DELETE_LICH_KHAM = 75;
	public static final int ONRESUME_LICH_SU_KHAM_CHUA_BENH = 76;
	public static final int REQ_EDIT_AVATAR_ASSISTION = 77;
	public static final int ADD_PATIENT = 78;
	public static final int SELECT_PHONE_CONTACT_ADD_PATIENT = 79;
	public static final int DELETE_PATIENTS = 80;
	public static final int ADD_PATIENT_DOCTOR = 81;
	
	public final static String TAG_CHILD = "TAG_CHILD";
	public final static String KEY = "KEY";
	public final static String PHONE = "PHONE";
	public final static String CUSTOMBAR = "CUSTOMBAR";

	public final static String KEY_TINHTHANH = "KEY_TINHTHANH";

	public final static String REQUEST_CODE = "REQUESTCODE";
	public final static String TITLE = "TITLE";
	public final static String TIME = "TIME";

	public final static String TAG_LICH_HEN = "TAG_LICH_HEN";
	public final static String TAG_LICH_KHAM = "TAG_LICH_KHAM";
	public final static String TAG_TINNHAN = "TAG_TINNHAN";
	public final static String TAG_BENHNHAN = "TAG_BENHNHAN";
	public final static String TAG_TAIKHOAN = "TAG_TAIKHOAN";

	public final static String TAG_HOME = "TAG_HOME";
	private final static String PREF_APP = "pref_common_app";
	public final static String KEY_USER = "KEY_USER";
	public final static String KEY_CITY = "KEY_CITY";
	public final static String KEY_TIME = "KEY_TIME";
	public final static String KEY_SEARCH = "KEY_SEARCH";
	public final static String KEY_IS_SEARCH = "KEY_IS_SEARCH";

	public final static String KEY_STATUS = "KEY_STATUS";
	public final static String KEY_ID = "KEY_ID";
	public final static String KEY_DOCTORID = "KEY_DOCTORID";
	public final static String KEY_SELECT_MONTH = "KEY_SELECT_MONTH";

	public final static String KEY_UPDATE = "KEY_UPDATE";

	public static final int LICH_TRONG = -2;

	public static HashMap<String, String> map = null;
	public static HashMap<String, String> mapDateVn = null;

	public final static int LOGIN_FORM = -1;
	public final static int SHOW_ACTION_MODD = 111;
	

	public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
			throws FileNotFoundException {
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri),
				null, o);

		int width_tmp = o.outWidth, height_tmp = o.outHeight;
		int scale = 1;

		while (true) {
			if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
				break;
			width_tmp /= 2;
			height_tmp /= 2;
			scale *= 2;
		}

		BitmapFactory.Options o2 = new BitmapFactory.Options();
		o2.inSampleSize = scale;
		return BitmapFactory.decodeStream(c.getContentResolver()
				.openInputStream(uri), null, o2);
	}

	public static String[] getCurrentDate(String pattern) {
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		Date date = new Date();
		String[] arValue = dateFormat.format(date).split(";");
		return arValue;
	}

	public static String getCurrentDayName(String pattern) {
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		Date date = new Date();
		String arValue = dateFormat.format(date);
		return arValue;
	}

	public static String getCurrentMonth() {
		String[] value = getCurrentDate("MM;dd;yyyy");
		return value[0];
	}

	public static String getCurrentDay() {
		String[] value = getCurrentDate("MM;dd;yyyy");
		return value[1];
	}

	public static boolean isCheckPassDay(String selectDate) {

		String currentDate = Utils.getCurrentDayName("yyyy-MM-dd");
		long selectTime = convertDate(selectDate, "yyyy-MM-dd").getTime();
		long currentTime = convertDate(currentDate, "yyyy-MM-dd").getTime();
		long tmp = selectTime - currentTime;
		if (tmp < 0) {
			return false;
		} else {
			return true;
		}
	}

	public static String getCurrentYear() {
		String[] value = getCurrentDate("MM;dd;yyyy");
		return value[2];
	}

	public static void disableEnableControls(boolean enable, View vgtmp) {
		ViewGroup vg = (ViewGroup) vgtmp;
		try {
			for (int i = 0; i < vg.getChildCount(); i++) {
				View child = vg.getChildAt(i);
				child.setEnabled(enable);
				if (child instanceof ViewGroup) {
					disableEnableControls(enable, (ViewGroup) child);
				}
			}
		} catch (Exception e) {

		}
	}

	public static List<ItemMeu> getListLocation(Address address) {
		List<ItemMeu> mList = new ArrayList<ItemMeu>();
		for (int i = 0; i < address.getData().getAddress().size(); i++) {
			if (address.getData().getAddress().get(i).getIs_active() == 1) {
				ItemMeu item = new ItemMeu();
				item.setRegId(address.getData().getAddress().get(i).getId());
				item.setTitle(address.getData().getAddress().get(i)
						.getAddress());
				mList.add(item);
			}
		}
		return mList;
	}

	public static String getContactPhone(Intent data, Activity activity) {
		String phone = null;
		try {
			Uri contactData = data.getData();
			Cursor contactCursor = activity.getContentResolver().query(
					contactData,
					new String[] { ContactsContract.Contacts._ID }, null, null,
					null);
			String id = null;
			if (contactCursor.moveToFirst()) {
				id = contactCursor.getString(contactCursor
						.getColumnIndex(ContactsContract.Contacts._ID));
			}
			contactCursor.close();

			Cursor phoneCursor = activity
					.getContentResolver()
					.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ "= ? ", new String[] { id }, null);
			if (phoneCursor.moveToFirst()) {
				phone = phoneCursor
						.getString(phoneCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			}
			phoneCursor.close();
			phone = phone.replace(" ", "");
			phone = phone.replace("+84", "0");
			phone = phone.replace("-", "");
			Pattern pattern = Pattern.compile("\\d*");
			Matcher matcher = pattern.matcher(phone);
			if (!matcher.matches()) {
				phone = "0000";
			}
		} catch (Exception e) {

		}

		return phone;

	}

	public static Date convertDate(String date, String pattern) {
		Date d = new Date();
		if (TextUtils.isEmpty(date)) {
			return d;
		}
		SimpleDateFormat fm = new SimpleDateFormat(pattern, Locale.US);
		try {
			d = fm.parse(date);
			return d;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return d;
		}
	}

	public static void viewNodata(Activity activity, final View viewbig,
			String message, String tag, String tile, int icon,
			final IRcallBak callBack) {
		final FrameLayout frameLayout = (FrameLayout) viewbig
				.findViewById(R.id.viewMain);
		final View view = activity.getLayoutInflater().inflate(
				R.layout.message_errors_wifi, null);

		view.setTag(tag);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		tv_title.setText(tile);
		ImageView img = (ImageView) view.findViewById(R.id.icon);
		img.setImageResource(icon);
		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);

		tv_message.setText(message);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				view.setVisibility(View.GONE);
				callBack.onDisplay(1, null);
			}
		});
		frameLayout.addView(view, 0);

	}

	public static void viewNodataLk(Activity activity, final View viewbig,
			String message, String tile, int icon, final IRcallBak callBack) {
		final View view = (View) viewbig.findViewById(R.id.view_none);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		tv_title.setText(tile);
		ImageView img = (ImageView) view.findViewById(R.id.icon);
		img.setImageResource(icon);
		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);

		tv_message.setText(message);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				view.setVisibility(View.GONE);
				callBack.onDisplay(1, null);
			}
		});

	}

	public static String getDesc(Activity activity, int id) {
		return activity.getResources().getString(id);
	}

	public static String getDateFormat(String date) {
		Date curDate = convertDate(date, "yyyy-MM-dd");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(curDate).toString();
	}

	public static String getRealPathFromURI(Context context, Uri contentURI) {
		if ("file".equalsIgnoreCase(contentURI.getScheme())) {
			return contentURI.getPath();
		}
		String result = "";
		String[] projection = { "_data" };
		Cursor cursor = context.getContentResolver().query(contentURI,
				projection, null, null, null);
		if (cursor == null) { // Source is Dropbox or other similar local file
								// path
			result = contentURI.getPath();
		} else if (cursor.moveToFirst()) {
			int idx = cursor.getColumnIndex("_data");
			result = cursor.getString(idx);
			cursor.close();
		}
		return result;
	}

	public static int convertDpToPixel(int dps, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();

		final float scale = metrics.density;
		int pixels = (int) (dps * scale + 0.5f);
		return pixels;
	}

	public static void showDropdow(final Activity activity,
			final List<Item> listData, final IRcallBak callBack, String title) {
		final Dialog dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(android.R.color.white);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(R.layout.dialogprice);
		dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);

		TextView lblTitle = (TextView) dialog.findViewById(R.id.lblTitle);
		if (!TextUtils.isEmpty(title)) {
			lblTitle.setText(title.toUpperCase());
		} else {
			lblTitle.setVisibility(View.GONE);
		}
		// dialog.getWindow().setLayout(width - 0, LayoutParams.MATCH_PARENT);

		ListView listView = (ListView) dialog.findViewById(R.id.listsale);

		final SpinAdapter adapter = new SpinAdapter(activity,
				R.layout.item_publicsher, listData, 1);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Bundle bundle = new Bundle();
				bundle.putString(Utils.KEY_TINHTHANH, listData.get(position)
						.getName());
				callBack.onDisplay(listData.get(position).getId(), bundle);
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	public static void showDropdowDistrict(
			final Activity activity,
			final List<com.anbinh.doctor.model.DistrictResponse.Data.Item> listData,
			final IRcallBak callBack, String title) {
		final Dialog dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(android.R.color.white);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(R.layout.dialogprice);
		dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);

		TextView lblTitle = (TextView) dialog.findViewById(R.id.lblTitle);
		if (!TextUtils.isEmpty(title)) {
			lblTitle.setText(title.toUpperCase());
		} else {
			lblTitle.setVisibility(View.GONE);
		}
		// dialog.getWindow().setLayout(width - 0, LayoutParams.MATCH_PARENT);

		ListView listView = (ListView) dialog.findViewById(R.id.listsale);

		DistrictAdapter adapter = new DistrictAdapter(activity,
				R.layout.item_publicsher, listData, 1);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Bundle bundle = new Bundle();
				bundle.putString(Utils.KEY_TINHTHANH, listData.get(position)
						.getName());
				callBack.onDisplay(listData.get(position).getId(), bundle);
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	public static DisplayImageOptions getOptions(Context activity) {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.icon256)
				.showImageOnFail(R.drawable.icon256)
				.showImageOnLoading(R.drawable.icon256)
				.resetViewBeforeLoading(true).cacheOnDisc(true)
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(new FadeInBitmapDisplayer(300)).build();
		return options;
	}

	public static String getDayName(int year, int month, int day) {

		Calendar calendar = Calendar.getInstance();
		// passing month-1 because 0-->jan, 1-->feb... 11-->dec
		calendar.set(year, month - 1, day);
		Date date = calendar.getTime();
		// Then get the day of week from the Date based on specific locale.
		String dayOfWeek = new SimpleDateFormat("EEEE", Locale.ENGLISH)
				.format(date);

		return dayOfWeek; // Friday
	}

	public static HashMap<String, String> getMapDate() {
		if (map == null || map.size() == 0) {
			map = new HashMap<String, String>();
			map.put("Monday", "T2");
			map.put("Tuesday", "T3");
			map.put("Wednesday", "T4");
			map.put("Thursday", "T5");
			map.put("Friday", "T6");
			map.put("Saturday", "T7");
			map.put("Sunday", "CN");
		}
		return map;
	}

	public static HashMap<String, String> getMapDateVn() {
		if (mapDateVn == null || mapDateVn.size() == 0) {
			mapDateVn = new HashMap<String, String>();
			mapDateVn.put("T2", "Thứ 2");
			mapDateVn.put("T3", "Thứ 3");
			mapDateVn.put("T4", "Thứ 4");
			mapDateVn.put("T5", "Thứ 5");
			mapDateVn.put("T6", "Thứ 6");
			mapDateVn.put("T7", "Thứ 7");
			mapDateVn.put("CN", "Chủ nhật");
		}
		return mapDateVn;
	}

	public static String getLastDay(int month, int year) {
		Calendar calendar = Calendar.getInstance();
		// passing month-1 because 0-->jan, 1-->feb... 11-->dec
		calendar.set(year, month - 1, 1);
		calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		Date date = calendar.getTime();
		DateFormat DATE_FORMAT = new SimpleDateFormat("MM;dd;yyyy");
		String[] arValue = DATE_FORMAT.format(date).split(";");
		return arValue[1];
	}

	public static void updateData(Context context, String key, Object obj) {
		Gson gson = new Gson();
		String data = gson.toJson(obj);
		SharedPreferences pref = context.getSharedPreferences(PREF_APP,
				Context.MODE_PRIVATE);
		Editor edit = pref.edit();
		edit.putString(key, data);
		edit.commit();
	}

	public static CityResponse getCityResponse(Context context, String key) {
		SharedPreferences pref = context.getSharedPreferences(PREF_APP,
				Context.MODE_PRIVATE);
		String data = pref.getString(key, "");
		if (TextUtils.isEmpty(data)) {
			return null;
		}
		Gson gson = new Gson();
		return gson.fromJson(data, CityResponse.class);
	}

	public static UserLogin getAccountModel(Context context, String key) {
		SharedPreferences pref = context.getSharedPreferences(PREF_APP,
				Context.MODE_PRIVATE);
		String data = pref.getString(key, "");
		if (TextUtils.isEmpty(data)) {
			return null;
		}
		Gson gson = new Gson();
		return gson.fromJson(data, UserLogin.class);
	}

	public static ArrayList<DoctorInfo> getTitleExend(Activity activity,
			int menu) {

		ArrayList<DoctorInfo> menuItems = null;
		try {
			menuItems = new ArrayList<DoctorInfo>();
			XmlResourceParser xpp = activity.getResources().getXml(menu);

			xpp.next();
			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {
					String elemName = xpp.getName();
					if (elemName.equals("item")) {
						String textId = xpp.getAttributeValue(
								"http://schemas.android.com/apk/res/android",
								"title");
						String iconId = xpp.getAttributeValue(
								"http://schemas.android.com/apk/res/android",
								"icon");
						String resId = xpp.getAttributeValue(
								"http://schemas.android.com/apk/res/android",
								"id");
						DoctorInfo item = new DoctorInfo();
						item.setTitle(textId);
						Log.d("textId", textId);
						item.setRegId(Integer.valueOf(resId.replace("@", "")));
						item.setIcon(Integer.valueOf(iconId.replace("@", "")));
						menuItems.add(item);

					}
				}
				eventType = xpp.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return menuItems;
	}

	public static ArrayList<ItemMeu> getMenu(Activity activity, int menu) {

		ArrayList<ItemMeu> menuItems = null;
		try {
			menuItems = new ArrayList<ItemMeu>();
			XmlResourceParser xpp = activity.getResources().getXml(menu);

			xpp.next();
			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_TAG) {
					String elemName = xpp.getName();
					if (elemName.equals("item")) {
						String textId = xpp.getAttributeValue(
								"http://schemas.android.com/apk/res/android",
								"title");
						String iconId = xpp.getAttributeValue(
								"http://schemas.android.com/apk/res/android",
								"icon");
						String resId = xpp.getAttributeValue(
								"http://schemas.android.com/apk/res/android",
								"id");
						ItemMeu item = new ItemMeu();
						item.setTitle(textId);
						Log.d("textId", textId);
						item.setRegId(Integer.valueOf(resId.replace("@", "")));
						item.setIcon(Integer.valueOf(iconId.replace("@", "")));
						menuItems.add(item);

					}
				}
				eventType = xpp.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return menuItems;
	}

	public static void showMessage(Activity activity, String message) {
		LayoutInflater inflater = activity.getLayoutInflater();
		View layout = inflater.inflate(R.layout.toast_message,
				(ViewGroup) activity.findViewById(R.id.toast_layout_root));
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(message);
		int screenWidth = activity.getWindowManager().getDefaultDisplay()
				.getWidth();
		LayoutParams layoutParams = new LayoutParams(screenWidth,
				Utils.convertDpToPixel(55, activity));
		text.setLayoutParams(layoutParams);

		Toast toast = new Toast(activity);
		toast.setGravity(Gravity.BOTTOM, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(layout);
		toast.show();

	}

	public static void getNewIntent(Activity activity, Bundle bundle) {
		Intent intent = new Intent(activity, DetailtActivity.class);
		intent.putExtras(bundle);
		activity.startActivityForResult(intent,
				bundle.getInt(Utils.REQUEST_CODE));
	}

	public static void getNewIntentToPatient(Activity activity, Bundle bundle) {
		Intent intent = new Intent(activity, PatientActivity.class);
		intent.putExtras(bundle);
		activity.startActivityForResult(intent,
				bundle.getInt(Utils.REQUEST_CODE));
	}

	public static void hideKeyboard(Activity activity) {
		try {
			InputMethodManager imm = (InputMethodManager) activity
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(activity.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void setOnTouchHideKeyboard(final Activity activity, View view) {

		// Set up touch listener for non-text box views to hide keyboard.
		if (!(view instanceof EditText)) {

			view.setOnTouchListener(new OnTouchListener() {

				public boolean onTouch(View v, MotionEvent event) {
					hideKeyboard(activity);
					return false;
				}

			});
		}

		// If a layout container, iterate over children and seed recursion.
		if (view instanceof ViewGroup) {

			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

				View innerView = ((ViewGroup) view).getChildAt(i);

				setOnTouchHideKeyboard(activity, innerView);
			}
		}
	}

	public static void goneKeyBoard(final Activity activity) {

		new CountDownTimer(600, 600) {

			@Override
			public void onTick(long millisUntilFinished) {
			}

			@Override
			public void onFinish() {
				Utils.hideKeyboard(activity);
			}
		}.start();
	}

	public static void showConfirmOnPause(final int schedules_pause_id,
			final Activity activity, final IRcallBak callBak) {
		final Dialog dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				android.R.color.transparent);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) activity).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_tamhoan);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);

		TextView btnCo = (TextView) dialog.findViewById(R.id.btnCo);
		btnCo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				onStartAppointment(callBak, schedules_pause_id, activity);
			}
		});

		TextView btnKo = (TextView) dialog.findViewById(R.id.btnKo);
		btnKo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		dialog.show();

	}

	public static void onStartAppointment(final IRcallBak calBak,
			int schedules_pause_id, Activity activity) {
		calBak.onDisplay(Utils.SHOW_PROCESS, null);
		UserLogin userLogin = getAccountModel(activity, Utils.KEY_USER);
		ServerController.getInstance(activity).requestOnstartAppointMent(
				userLogin.getData().getToken(), schedules_pause_id,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						calBak.onDisplay(Utils.GONE_PROCESS, null);
						if (data.getResponsecode() >= 0) {
							calBak.onDisplay(Utils.SUSSCESS, null);
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						calBak.onDisplay(Utils.SHOW_PROCESS, null);
					}
				});

	}

	public static void onLoadding(View mView, boolean isLoadding) {
		View mLoading = (View) mView.findViewById(R.id.loadding);
		if (isLoadding) {
			mLoading.setVisibility(View.VISIBLE);
		} else {
			mLoading.setVisibility(View.INVISIBLE);
		}
		Utils.disableEnableControls(!isLoadding, mView);
	}

	public static void pickFile(Activity activity, int requestCode) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("file/*");
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		activity.startActivityForResult(intent, requestCode);

	}

	public static String formatMoney(long s) {
		NumberFormat usFormat = NumberFormat.getIntegerInstance(Locale.US);
		String money = usFormat.format(s);
		money = money.replace(",", ".");
		return money;
	}

	public static void pickFileImage(Activity activity, int requestCode) {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		activity.startActivityForResult(intent, requestCode);

	}
}
